package com.example.smartmedicalsystem.controller;

import com.example.smartmedicalsystem.entity.Message;
import com.example.smartmedicalsystem.entity.User;
import com.example.smartmedicalsystem.repository.MessageRepository;
import com.example.smartmedicalsystem.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/api/messages")
public class MessageController {
    private final MessageRepository messageRepository;
    private final UserRepository userRepository;

    public MessageController(
            MessageRepository messageRepository,
            UserRepository userRepository
    ) {
        this.messageRepository = messageRepository;
        this.userRepository = userRepository;
    }

    @GetMapping
    public List<Message> getMessages(Authentication authentication) {
        User user = userRepository.findByUsername(authentication.getName());
        return messageRepository.findByReceiverOrderByCreatedAtDesc(user);
    }
}